package cn.fl.npspl.service.impl;

import java.util.List;

import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.constant.WebBaseConstant;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.npspl.service.ISplNpBscInfoService;
import cn.fl.npspl.transform.SplNpBscInfoVOTransform;
import cn.fl.npspl.transform.SplNpSocialRlVOTransform;
import cn.fl.npspl.vo.SplNpBscInfoVO;
import cn.fl.npspl.vo.SplNpSocialRlVO;
import fl.spl.facade.framework.constant.SplSystemConstant;
import fl.spl.facade.npspl.api.ISplNpBscInfoApiService;
import fl.spl.facade.npspl.api.ISplNpSocialRlApiService;
import fl.spl.facade.npspl.dto.SplNpBscInfoDTO;


/**
 * @Description: 自然人经销商详细信息表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class SplNpBscInfoServiceImpl implements ISplNpBscInfoService {

    @Autowired
    private ISplNpBscInfoApiService splNpBscInfoApiService;
    @Autowired
    private ISplNpSocialRlApiService splNpSocialRlApiService;
    
    @Override
    public SplNpBscInfoVO selectById(Long id) throws BaseException {
        if (StringUtils.isEmpty(id)) {
            BaseLogUtils.newWebLogger("主键参数为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
            throw new BaseException(BaseExceptionConstant.COMMON_0001, "获取对象主键不能为空!");
        }
        //TODO 业务逻辑
        SplNpBscInfoDTO dto = this.splNpBscInfoApiService.selectSplNpBscInfoDTOById(id, CurrentThreadContext.getCurrentSysCd(), 
        		CurrentThreadContext.getCurrentOrgCd(), SplSystemConstant.FL_WEB_CHL_CD);
        return SplNpBscInfoVOTransform.toVO(dto);
    }

    @Override
    public Long add(SplNpBscInfoVO entity) throws BaseException {
        if (null == entity) {
            BaseLogUtils.newWebLogger("新增对象不存在").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
            throw new BaseException(BaseExceptionConstant.COMMON_0001, "新增对象不存在!");
        }
        
         Long id = this.splNpBscInfoApiService.addSplNpBscInfoDTO(SplNpBscInfoVOTransform.toDTO(entity), CurrentThreadContext.getFlServiceSysCd()
        		, CurrentThreadContext.getCurrentOrgCd(), entity.getDsCd());
         
         if (StringUtils.isNotEmpty(id) && 0L != id) {
			List<SplNpSocialRlVO> rlPsnList = entity.getRlPsnList();
			if (CollectionUtils.isEmpty(rlPsnList)) {
				throw new BaseException(BaseExceptionConstant.COMMON_0001, "请维护联系人!");
			}
			for (SplNpSocialRlVO splNpSocialRlVO : rlPsnList) {
				splNpSocialRlVO.setCstId(id);
				splNpSocialRlVO.setIsValid(BaseConstant.IS_YESNO_YES);
			}
			this.splNpSocialRlApiService.batchAddAndModify(SplNpSocialRlVOTransform.toDTOList(rlPsnList));
		}
        
        return id;
    }

    @Override
    public boolean removeById(Long id) throws BaseException {
        if (StringUtils.isEmpty(id)) {
            throw new BaseException(BaseExceptionConstant.COMMON_0001, "删除对象主键不能为空!");
        }
        ;
        //return this.splNpBscInfoApiService.removeSplNpBscInfoDTOById(id);
        //TODO 业务逻辑
        return false;
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @Override
    public boolean removeByIds(List<Long> ids) throws BaseException {
        if (CollectionUtils.isEmpty(ids)) {
            throw new BaseException(BaseExceptionConstant.COMMON_0001, "删除对象主键不能为空!");
        }
        ;
        return false;
    }

    @Override
    public boolean modify(SplNpBscInfoVO entity) throws BaseException {
        if (null == entity) {
            BaseLogUtils.newWebLogger("修改对象不能为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
            throw new BaseException(BaseExceptionConstant.COMMON_0001, "修改对象不能为空!");
        }
        ;
        boolean success =  this.splNpBscInfoApiService.modifySplNpBscInfoDTO(SplNpBscInfoVOTransform.toDTO(entity), CurrentThreadContext.getFlServiceSysCd(), 
        		CurrentThreadContext.getCurrentOrgCd(), entity.getDsCd());

        //if (success) {
        List<SplNpSocialRlVO> rlPsnList = entity.getRlPsnList();
        if (CollectionUtils.isEmpty(rlPsnList)) {
            throw new BaseException(BaseExceptionConstant.COMMON_0001, "请维护联系人!");
        }
        for (SplNpSocialRlVO splNpSocialRlVO : rlPsnList) {
            splNpSocialRlVO.setCstId(entity.getId());
            splNpSocialRlVO.setIsValid(BaseConstant.IS_YESNO_YES);
        }
        this.splNpSocialRlApiService.batchAddAndModify(SplNpSocialRlVOTransform.toDTOList(rlPsnList));
		//}
       
        return true;
    }
    
    @Override
    public Pager searchListPage(Pager page) throws BaseException {
        if (null == page) {
            page = new Pager();
        }
        //return this.splNpBscInfoApiService.searchSplNpBscInfoListPage(page);
        //TODO 业务逻辑
        return null;
    }

	@Override
	public void checkCredit(Long splId, String ip) throws BaseException {
		String chlCd = SplSystemConstant.FL_WEB_CHL_CD;
		String crtUsrId = CurrentThreadContext.getCurrentUserId() + "";
		String crtUsrNm = CurrentThreadContext.getCurrentUserName();
		String sysCd = WebBaseConstant.SYS_SYS_CD;
		String serviceCd = SplSystemConstant.SPL_SERVICE_CD;
		
		this.splNpBscInfoApiService.checkCredit(splId, ip, sysCd, chlCd, serviceCd, crtUsrId, crtUsrNm);
	}
}
